var states = [{code: 1, text: '开启' }, {code: 2, text: '关闭'}];

mini.parse();
var grid = mini.get('datagrid1');                               //显示数据表格
var form = new mini.Form('form1');                                   //下拉框form
var targetFormEl = mini.getByName('targetForm');                //目标单据
var targetFormFiledEl = mini.getByName('targetFormFiled');      //目标单据字段
var sourceFormEl = mini.getByName('sourceForm');                //源单据
var sourceFormFiledEl = mini.getByName('sourceFormFiled');      //源单据字段
var primarytable = mini.getByName('primarytable');

var newArr = [];  // 表格中文数据存放
var newylArr=[];  // 表格英文数据存放
//当选择下拉选项时
var modelId = localStorage.getItem("modelId");
modelId =JSON.parse(modelId);
var cTableName;
function onvaluechanged(e) {
    console.log(e);
}
//确定
function onOk() {

    // 行数据存放
    var obj = {};
    // 行赋值
    obj.targetForm = targetFormEl.text;
    obj.targetFormyEnglish=targetFormEl.getValue();
    obj.targetFormFiled = targetFormFiledEl.text;
    obj.targetFormFiledEnglish=targetFormEl.getValue();
    obj.sourceForm = sourceFormEl.text;
    obj.sourceFormyEnglish=sourceFormEl.getValue();
    obj.sourceFormFiled = sourceFormFiledEl.text;
    obj.sourceFormFiledEnglish=sourceFormFiledEl.getValue();
    obj.primarytable=primarytable.text;
    console.log(obj);
    if(obj.targetForm==''||obj.targetFormFiled==''||obj.sourceForm=='' ||obj.primarytable==''||obj.sourceFormFiled==''){
        mini.alert("请全选");
        return;
    }
    newArr.push(obj);
    // 表格赋值
    console.log(newArr);
    grid.setData(newArr);
    var selectVal = form.getData();
    newylArr.push(selectVal);
}
// 提交
function  onSubmit() {
    console.log(newylArr,'提交结果');
    cTableName = sourceFormEl.getValue();
    console.log(cTableName);
    newylArr=JSON.stringify(newylArr);
    console.log(newylArr);
    // console.log(typeof newArry);
    $.ajax({
        url:ajaxUrl+"/rule/SetValue.do",
        type:"post",

        data:{
            newylArr:newylArr,
            modelId:modelId,
            cTableName:cTableName
        },
        success:function (data) {
            console.log(data);
            mini.alert(data.state)
        },
        error:function () {
        }
    })
}
function requestcom() {

    $.ajax({
        url:ajaxUrl+"/rule/findByModelId.do",
        type:"post",
        data:{modelId:modelId},
        success:function (data) {
            console.log(data);
            targetFormEl.setData(data.data);

        },
        error:function () {
        }
    })
}
function onvaluechanged(){
    var data=mini.get("targetForm1").getValue();  //获得下拉框选择的对应的值
    console.log(data);
    $.ajax({
        url:ajaxUrl+"/rule/findBycTableName.do",
        type:"post",
        data:{cTableName:data},
        success:function (data) {
            console.log(data);
            targetFormFiledEl.setData(data.data)

        },
        error:function () {
        }
    })
}
function requestbobox() {

    $.ajax({
        url:ajaxUrl+"/rule/findAll.do",
        type:"post",

        success:function (data) {
            console.log(data);
            sourceFormEl.setData(data.data);

        },
        error:function () {

        }
    })
}
function onvaluechanged2() {

    var data2=mini.get("sourceForm1").getValue();  //获得下拉框选择的对应的值
    console.log(data2);
    $.ajax({
        url:ajaxUrl+"/rule/findTableByName.do",
        type:"post",
        data:{cName:data2},
        success:function (data) {
            console.log(data);
            primarytable.setData(data.data)

        },
        error:function () {
        }
    })
}
function onvaluechanged3() {

    var data2=mini.get("primarytable1").getValue();  //获得下拉框选择的对应的值
    console.log(data2);
    $.ajax({
        url:ajaxUrl+"/rule/findBycTableName.do",
        type:"post",
        data:{cTableName:data2},
        success:function (data) {
            console.log(data);
            sourceFormFiledEl.setData(data.data)

        },
        error:function () {
        }
    })
}
function onRemove() {
    var selected = grid.getSelecteds();
    if (selected.length>0){
        mini.confirm("确定删除记录？","慎选",function (action) {
            if(action=="ok"){
                grid.removeRows(selected);
            }
        })
    } else {
        mini.alert("请您选择一条")
    }
}